cmake_minimum_required(VERSION 3.10)
project(VSomeIp)

# ================= Dynamic Root & Output Layout =====================
# Expect environment variable ADAPTIVE_ROOT exported by env.sh.
# We derive all dependency roots from that to avoid hard-coded
# absolute paths that break when the workspace is relocated.
# ====================================================================

if (NOT DEFINED ENV{ADAPTIVE_ROOT})
	message(FATAL_ERROR "ADAPTIVE_ROOT not set in environment. Source env.sh first.")
endif()

set(ADAPTIVE_ROOT $ENV{ADAPTIVE_ROOT})
set(OUTPUT_LINUX      ${ADAPTIVE_ROOT}/98_output/Linux)
set(OUTPUT_M55        ${ADAPTIVE_ROOT}/98_output/M55)
set(BOOST_ROOT_DIR    ${OUTPUT_LINUX}/boost)
set(VSOMEIP_ROOT_DIR  ${OUTPUT_LINUX}/vsomeip)

# Provide include/lib roots
set(BOOST_INCLUDE_DIR ${BOOST_ROOT_DIR}/include)
set(BOOST_LIB_DIR     ${BOOST_ROOT_DIR}/lib)
set(VSOMEIP_INCLUDE_DIR ${VSOMEIP_ROOT_DIR}/include)
set(VSOMEIP_LIB_DIR     ${VSOMEIP_ROOT_DIR}/lib)

# Point CMake package lookup for vsomeip3 to the installed config
set(vsomeip3_DIR ${VSOMEIP_LIB_DIR}/cmake/vsomeip3 CACHE PATH "Path to installed vsomeip3 cmake config")

find_package(vsomeip3 REQUIRED)

include_directories(
	${BOOST_INCLUDE_DIR}
	${VSOMEIP_INCLUDE_DIR}
)

link_directories(
	${BOOST_LIB_DIR}
	${VSOMEIP_LIB_DIR}
)

# RPATH so that deployed binaries locate boost & vsomeip without LD_LIBRARY_PATH
set(CMAKE_BUILD_RPATH "${BOOST_LIB_DIR};${VSOMEIP_LIB_DIR}")
set(CMAKE_INSTALL_RPATH "${BOOST_LIB_DIR};${VSOMEIP_LIB_DIR}")
set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

link_directories(
  ${BOOST_LIB_DIR}
  ${VSOMEIP_LIB_DIR}
)

find_package(vsomeip3)
if (NOT vsomeip3_FOUND)
    message("vsomeip3 was not found. Please specify vsomeip_DIR")
endif()

add_subdirectory(VsomeipServer)
add_subdirectory(VsomeipClient)
